package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

//登录页面
public class LoginPage extends Utils {
    public static String url = "http://8.156.68.126:8080/blog_login.html";

    public LoginPage() {
        super(url);
    }

    //检查页面是否可以正常打开
    public void checkPageRight(){
        //检擦导航是否加载成功
        driver.findElement(By.cssSelector("body > div.nav > span"));

        //检查登录框
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    /**
     * 异常登录
     * ---用户名和密码都为空
     * ---用户名为空，密码不为空
     * ---用户名不为空，密码为空
     * ---输入正确的账号，错误的密码
     * ---输入错误的账号（不存在），错误的密码
     * ---输入错误的账号，正确的密码
     * ---输入框：输入过长的、过短的、特殊字符....
     */
    public void LoginFail(){
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        //处理错误的弹窗---警告弹窗
        wait.until(ExpectedConditions.alertIsPresent());

        Alert alert = driver.switchTo().alert();
        String actualMsg = alert.getText();
//        System.out.println(actualMsg);
        alert.accept();

        String expectMsg = "发生错误:密码错误";
        assert actualMsg.equals(expectMsg);
    }

    public void LoginSuc(){
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //登陆成功会进入到列表页---注销菜单
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
    }
}
